package org.zxw.controller.userSystem;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import org.zxw.openFeign.TrainingPlanClient;
import org.zxw.pojo.entity.TrainingPlan;
import org.zxw.pojo.entity.User;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/user/recommond")
public class TrainingPlanController {
    // 通过webclient调用TrainPlan模块的服务
    private WebClient.Builder webClientBuilder;
    private TrainingPlanClient trainingPlanClient;

    public TrainingPlanController(WebClient.Builder webClientBuilder, TrainingPlanClient trainingPlanClient) {
        this.webClientBuilder = webClientBuilder;
        this.trainingPlanClient = trainingPlanClient;
    }


    /**
     * 基础推荐接口
     * @param goal
     * @param limit
     * @return
     */
    @GetMapping("/foundation")
    public ResponseEntity<List<TrainingPlan>> recommendPlan(
            @RequestParam String goal,
            @RequestParam(defaultValue = "3") int limit) {
//        return webClientBuilder.baseUrl("http://localhost:8081").build().get().
//                uri(uriBuilder -> uriBuilder
//                        .path("/planRecommend/foundation")
//                        .queryParam("goal", goal)
//                        .queryParam("limit", limit)
//                        .build())
//                .retrieve()
//                .toEntityList(TrainingPlan.class) // 直接获取 ResponseEntity<List<TrainingPlan>>
//                .block(); // 如果使用 WebFlux，需要调用 block() 方法来获取响应
        return trainingPlanClient.recommendPlan(goal, limit);
    }

    /**
     * 高级推荐接口
     * @param goal
     * @param difficulty
     * @param limit
     * @return
     */
    @GetMapping("/advanced")
    public ResponseEntity<List<TrainingPlan>> recommendPlanAdvanced(
            @RequestParam String goal,
            @RequestParam String difficulty,
            @RequestParam(defaultValue = "3") int limit) {
//        return webClientBuilder.baseUrl("http://localhost:8081").build().get().
//                uri(uriBuilder -> uriBuilder
//                        .path("/planRecommend/advanced")
//                        .queryParam("goal", goal)
//                        .queryParam("difficulty", difficulty)
//                        .queryParam("limit", limit)
//                        .build())
//                .retrieve()
//                .toEntityList(TrainingPlan.class) // 直接获取 ResponseEntity<List<TrainingPlan>>
//                .block(); // 如果使用 WebFlux，需要调用 block() 方法来获取响应
        return trainingPlanClient.recommendPlanAdvanced(goal, difficulty, limit);
    }

    /**
     * 智能推荐接口 -根据BMI和难度
     * @param height
     * @param weight
     * @param difficulty
     * @param limit
     * @return
     */
    @GetMapping("/smart")
    public ResponseEntity<List<TrainingPlan>> recommendPlanSmart(
            @RequestParam double height, // 单位:米
            @RequestParam double weight, // 单位:千克
            @RequestParam(defaultValue = "中级") String difficulty,
            @RequestParam(defaultValue = "3") int limit) {
//        return webClientBuilder.baseUrl("http://localhost:8081").build().get().
//                uri(uriBuilder -> uriBuilder
//                        .path("/planRecommend/smart")
//                        .queryParam("height", height)
//                        .queryParam("weight", weight)
//                        .queryParam("difficulty", difficulty)
//                        .queryParam("limit", limit)
//                        .build())
//                .retrieve()
//                .toEntityList(TrainingPlan.class) // 直接获取 ResponseEntity<List<TrainingPlan>>
//                .block(); // 如果使用 WebFlux，需要调用 block() 方法来获取响应
        return trainingPlanClient.recommendPlanSmart(height, weight, difficulty, limit);
    }
}
